package com.infore.dataAudit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.infore.dataAudit.conf.ProcessDeployAutoSettings;
import com.infore.dataAudit.remote.IBaseDataService;
import com.infore.dataAudit.repository.ITDataAuditRepository;
import com.infore.dataAudit.repository.ITTestClassRepository;
import com.infore.dataAudit.repository.ITTestStudentRepository;
import com.infore.dataAudit.service.DeployService;
import com.infore.dataAudit.util.Constants;
import com.infore.dataAudit.util.ResultEntity;

@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired  
    private RepositoryService repositoryService; 
    @Autowired  
    private RuntimeService runtimeService;  
    @Autowired  
    private TaskService taskService;  
    @Resource
    private ObjectMapper objectMapper;
    @Autowired
    private ITDataAuditRepository dataAuditRepository;
    @Autowired
    private ITTestClassRepository testClassRepository;
    @Autowired
    private ITTestStudentRepository testStudentRepository;
    @Autowired
    private IBaseDataService baseDataService;
    @Autowired
    private ProcessDeployAutoSettings processDeployAutoSettings;
    
    @Autowired
    private DeployService deployService;
    
    
    @RequestMapping("/firstDemo1")
    public String firstDemo1() {
        System.out.println("流程key " + processDeployAutoSettings.getDataAuditKey());
        return processDeployAutoSettings.getDataAuditKey();
    }
    
    @RequestMapping("/zdStartAuditFlow")
    public void zdStartAuditFlow(String time, String password) {
        if("cys123".equals(password))
            deployService.startAuditFlow(time);
    }
    
    @RequestMapping("/firstDemo2")
    public void firstDemo2() {
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("processes/dataAudit2.bpmn").deploy();
        System.out.println("部署成功! " +deployment.getId());
    }
    
    
    //jpa 级联测试
    @RequestMapping("/testClass")
    public ResultEntity testClass() {
        //List<Map<String,Object>> list = dataAuditRepository.queryDataAuditGroupByTime("mn1908", "2019-03-01", "2019-06-01");
        List<String> stationMnList = new ArrayList<>();
        stationMnList.add("10019");
        stationMnList.add("10019");
        return baseDataService.stationInfoAudit(stationMnList);
    }
    
    @RequestMapping("/firstDemo")
    public void firstDemo() throws InterruptedException {
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //SimpleDateFormat sdfISO = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
        //根据bpmn文件部署流程
        //Deployment deployment = repositoryService.createDeployment().addClasspathResource("processes/demo1.bpmn").deploy();
        //获取流程定义
        //ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        //启动流程定义，返回流程实例
        //System.out.println("流程定义ID："+processDefinition.getId());
        //ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());
        Map<String,Object> params = new HashMap<>();
        params.put("oneTimeEnd", "2019-03-20T12:00:11");
        params.put("oneTimeEnd2", "2019-03-20T11:45:11");
        //ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId(), params);
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("audioFlow01", params);
        String processId = pi.getProcessInstanceId();
        System.out.println("流程创建成功，当前流程实例ID："+processId);
        
      //  runtimeService.setVariable(pi.getSuperExecutionId(), "oneTimeEnd", "2019-12-30T11:11:11");
        
        Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("第一次执行前，任务名称："+task.getName());
        
    }
}
